home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / unix / chdir.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  984b  |  62 lines

  1.  
  2. /*
  3.  *  CHDIR.C
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <libraries/dos.h>
  12. #include <clib/dos_protos.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. #ifndef UnixToAmigaPath
  17. #define UnixToAmigaPath(path)   path
  18. #endif
  19.  
  20. typedef struct FileInfoBlock FIB;
  21.  
  22. static BPTR OrigDir;
  23.  
  24. static void
  25. chdir_exit()
  26. {
  27.     if (OrigDir) {
  28.     UnLock(CurrentDir(OrigDir));
  29.     OrigDir = NULL;
  30.     }
  31. }
  32.  
  33. int
  34. chdir(path)
  35. const char *path;
  36. {
  37.     int r = -1;
  38.     BPTR lock;
  39.  
  40.     if (lock = Lock(UnixToAmigaPath(path), SHARED_LOCK)) {
  41.     FIB *fib;
  42.  
  43.     if (fib = malloc(sizeof(FIB))) {
  44.         if (Examine(lock, fib) && fib->fib_DirEntryType > 0) {
  45.         r = 0;
  46.         lock = CurrentDir(lock);
  47.         if (OrigDir == NULL) {
  48.             OrigDir = lock;
  49.             lock = NULL;
  50.             atexit(chdir_exit);
  51.         }
  52.         }
  53.         free(fib);
  54.     }
  55.     if (lock)
  56.         UnLock(lock);
  57.     }
  58.     return(r);
  59. }
  60.  
  61.  
  62.